<?php
require_once "BaseAction.php";
require_once 'lib/thrift/client/VideoService.php';

class Event_VideoController extends TTEvent_Base_Action 
{
	
	public function init(){
		parent::init();
		$this->_helper->layout->setLayout('event-detail');
		$this->_helper->eventCommon->buildMainMenuBlock();
	}
	
	// video list
	public function indexAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
        
        $this->setTitle("Video | ".$event->event_name);
		$user = $this->getUser();
		$req = $this->getRequest();
		
		if(!$this->_helper->privacy->isEnable('video', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		if ($req->isPost())
		{
			$video_delete = $req->getPost('video_delete', 0);
			$vids = $req->getPost('vids', array());
			if ($video_delete==1)
			{
				if(!empty($vids)){
					$this->delEventVideos($eid, $vids);
					$changed = array('changed'=>time(), 'video_count'=>$this->getEventVideoCount($eid));
					$this->_helper->modelLoader->changeEventInfo($eid, $changed);
					$this->view->removeSuccess = 1;
					$this->view->removeCount = count($vids);
				}
			}
		}
		
		$page = $req->getQuery('page', 1);
		$limit = 20;
		
		
		$total = $this->getEventVideoCount($eid);
		$totalPage = ceil($total/$limit);
		if($page > $totalPage)
			$page = $totalPage;
		$eventVideos = $this->getEventVideos($eid, $page, $limit);
		
		$this->view->videoList = $eventVideos;
		$this->view->totalVideo = $total;
		$this->view->limit = $limit;
		$this->view->uid = $user->uid;
		$this->view->pager = $this->view->partial("pager-new.phtml", array('page'=>$page, "url" => "/event/video/" . $event->eid, "param" => "", 'haveNext'=>(count($eventVideos)>$limit)));
		$this->view->event = $event;
		$this->view->page = $page;
		
		$this->view->isAdmin  = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		$this->view->action = 'list';
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
		
	}
	
	public function delEventVideos($eid, $vids){
		$eid = (int)$eid;
		if($eid && is_array($vids) && count($vids)>0){
			$success = false;
			try {
				$v_client = new VideoService(VIDEO_THRIFT_SERVER, VIDEO_THRIFT_PORT);
				$v_client->OpenTransport();
				$v_client->client()->removeEventVideos($eid, $vids);
				$v_client->CloseTransport();
				$success = true;
			} catch (Exception $ex){
				//$videos = array();
				$success = false;
			}
			if($success){
				$this->clearEventVideoCache($eid);
			}
		}
	}
	
	public function addEventVideos($eid, $vids, $uid){
		
		$eid = (int)$eid;
		if($eid && is_array($vids) && count($vids)>0){
			$success = false;
			try {
				$v_client = new VideoService(VIDEO_THRIFT_SERVER, VIDEO_THRIFT_PORT);
				$v_client->OpenTransport();
				$v_client->client()->addEventVideos($eid, $vids, $uid);
				$v_client->CloseTransport();
				$success = true;
			} catch (Exception $ex){
				$success = false;
				//$videos = array();
			}
			if($success){
				$this->clearEventVideoCache($eid);
			}
		}
	}
	
	
	public function addAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
        
        $this->setTitle("Thêm video | ".$event->event_name);
		$user = $this->getUser();
		if(!$this->_helper->privacy->isCanAdd('video', $eid, $user->uid)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		if(!$this->_helper->privacy->isEnable('video', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		$req = $this->getRequest();
		
		if ($req->isPost())
		{
			$vids = $req->getPost('vids', array());
			$video_add = $req->getPost('video_add', 0);
			$vids = $req->getPost('vids', array());
			
			if ($video_add==1)
			{
				if(!empty($vids)){
					$this->addEventVideos($eid, $vids, $user->uid);
					$changed = array('changed'=>time(), 'video_count'=>$this->getEventVideoCount($eid));
					$this->_helper->modelLoader->changeEventInfo($eid, $changed);
					$this->view->addSuccess = 1;
					$this->view->addCount = count($vids);
				}
			}
		}
		$page = $req->getQuery('page', 1);
		$limit = 20;
		
		$eventVideos = $this->getEventUserVideo($user->uid, $eid, $page, $limit);
		
		$this->view->videoList = $eventVideos;
		$this->view->limit = $limit;
		$this->view->uid = $user->uid;
		$this->view->pager = $this->view->partial("pager-new.phtml", array('page'=>$page, "url" => "/event/video/add/" . $event->eid, "param" => "", 'haveNext'=>(count($eventVideos)>$limit)));
		$this->view->event = $event;
		$this->view->page = $page;
		
		$this->view->isAdmin  = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		$this->view->action = 'list';
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
	}
	
	public function getEventVideoCount($eid){
		$v_client = null;
		$total = 0;
		
		try {
			$v_client = new VideoService(VIDEO_THRIFT_SERVER, VIDEO_THRIFT_PORT);
			$v_client->OpenTransport();
			$total = $v_client->client()->getEventVideoCount($eid);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$videos = array();
		}
		return $total;
	}
	
	public function getEventVideos($eid, $page=1, $limit=20){
		
		$v_client = null;
		$eventVideos = array();
		$page = (int)$page;
		if($page < 1) $page = 1;
		
		try {
			$v_client = new VideoService(VIDEO_THRIFT_SERVER, VIDEO_THRIFT_PORT);
			$v_client->OpenTransport();
			$eventVideos = $v_client->client()->getEventVideo($eid, $page, $limit);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$videos = array();
		}
		return $eventVideos;
	}
	
	public function getUserVideoCount($uid){
		$v_client = null;
		$total = 0;
		
		try {
			$v_client = new VideoService(VIDEO_THRIFT_SERVER, VIDEO_THRIFT_PORT);
			$v_client->OpenTransport();
			$total = $v_client->client()->getUserVideoCount($uid);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$videos = array();
		}
		return $total;
	}
	
	public function getEventUserVideo($uid, $eid, $page, $limit){
		$v_client = null;
		$eventVideos = array();
		
		try {
			$v_client = new VideoService(VIDEO_THRIFT_SERVER, VIDEO_THRIFT_PORT);
			$v_client->OpenTransport();
			$eventVideos = $v_client->client()->getUserEventVideo($uid, $eid, $page, $limit);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$videos = array();
		}
		return $eventVideos;
		
	}
	
	private function clearEventVideoCache($eid){
		$this->deleteCache("html_event_video_block_".$eid);
	}
}

